package com.zmn.oms.common.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 签名
 *
 * @author liuying
 * @date 2018/12/25 8:31 PM
 */
public class SignUtil {

    private static final Logger logger = LoggerFactory.getLogger(SignUtil.class);

    /**
     * 对paramValues进行签名
     *
     * @param paramValues
     * @param secret
     * @return
     */
    public static String signMd5(Map<String, String> paramValues, String secret) {
        StringBuilder sb = new StringBuilder();
        List<String> paramNames = new ArrayList<String>(paramValues.size());
        paramNames.addAll(paramValues.keySet());
        Collections.sort(paramNames);

        for (String paramName : paramNames) {
            sb.append(paramName).append(paramValues.get(paramName) == null ? "" : paramValues.get(paramName));
        }

        sb.append(secret);
        logger.info("interface#request#sign-src={}", sb);
        return DigestUtils.md5Hex(sb.toString());
    }

}
